﻿
Partial Class WebForm_SearchControl_SearchEmployeeControl
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Visible = False
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
        If e.CommandName = "Select" Then
            Dim rowIndex As Integer = e.CommandArgument
            Me.SelectedEmployeeID = Me.gv.DataKeys(rowIndex)("EmployeeID")
            Me.SelectedEmployeeName = TryCast(Me.gv.Rows(rowIndex).FindControl("txtEmployeeName"), Label).Text
            Me.SelectedEmployeeTEL = TryCast(Me.gv.Rows(rowIndex).FindControl("lblPhoneExtNumber"), Label).Text

            RaiseEvent SelectedIndexChanged(Me, New EventArgs)
        End If
    End Sub

    Protected Sub ddlOrganizationID_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlOrganizationID.Load
        If Not IsPostBack Then
            Me.ddlOrganizationID.DataTextField = "OrganizationName"
            Me.ddlOrganizationID.DataValueField = "OrganizationID"
            Me.ddlOrganizationID.DataSource = MyObj.GetDDLOrganizationData()
            Me.ddlOrganizationID.DataBind()
        End If
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
    End Sub
End Class

Partial Class WebForm_SearchControl_SearchEmployeeControl
    Public Sub Show()
        Me.BindGV()
    End Sub

    Private Sub BindGV()
        Dim dat As New clsData
        Dim sql As MyStringBuilder

        If Me.AuthRequired = True Then
            sql = New MyOrganizationDataFilter
        Else
            sql = New MyStringBuilder
        End If

        sql.Length = 0
        sql.AppendFormat("select e.*, o.OrganizationName ")
        sql.AppendFormat(" from Employee e ")
        sql.AppendFormat("inner join Organization o on e.OrganizationID = o.OrganizationID ")
        sql.AppendFormat("where e.Active = 'Y' ")

        sql.AppendFormat(Me.GetWhereSQL())

        Me.gv.DataSource = dat.GetData(sql)
        Me.gv.DataBind()

        Me.Visible = True
        Me.mpSearch.Show()
    End Sub

    Public Function GetWhereSQL() As String
        Dim sql As New MyStringBuilder

        If Me.ddlOrganizationID.SelectedValue <> "" Then
            sql.AppendFormat("and e.OrganizationID = {0} ", Me.ddlOrganizationID.SelectedValue)
        End If

        Return sql.ToString()
    End Function

    Public Property SelectedEmployeeID() As String
        Get
            Return ViewState("SelectedEmployeeID")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedEmployeeID") = value
        End Set
    End Property

    Public Property SelectedEmployeeName() As String
        Get
            Return ViewState("SelectedEmployeeName")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedEmployeeName") = value
        End Set
    End Property

    Public Property SelectedEmployeeTEL() As String
        Get
            Return ViewState("SelectedEmployeeTEL")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedEmployeeTEL") = value
        End Set
    End Property

    Public Property AuthRequired() As Boolean
        Get
            If ViewState("AuthRequired") Is Nothing Then
                Return True
            End If

            Return ViewState("AuthRequired")
        End Get
        Set(ByVal value As Boolean)
            ViewState("AuthRequired") = value
        End Set
    End Property

    Public Event SelectedIndexChanged(ByVal source As Object, ByVal e As EventArgs)
End Class